package ce.com.cenewbluesdk.entity.k3;

import android.os.Parcel;
import android.os.Parcelable;

import ce.com.cenewbluesdk.uitl.ByteParser;
import ce.com.cenewbluesdk.uitl.TimeUtil;

/**
 * Created by Administrator on 2016/11/22.
 */
public class Mix_sport_Struct implements MixSportType,Parcelable {

    int sport_type;

    private long startTime;// start time   4
    /** 持续时间, 设备侧传过来的单位是1/100秒, 解析时要除100 */
    private int duration;// 持续时间   4
    private int distance;//   2 or 4
    private int avgBpm;// 心率
    private int count;// 步数/次数/划水次数，次数  2
    private int calories;// 2

    private int speed;// 2
    private int laps;// 游泳趟数    2
    private int validDuration;// 有效游泳时间：4
    private int swolf;// 高尔夫系数   2
    private int avgSpm;// 平均划水数  2

    protected Mix_sport_Struct(Parcel in) {
        sport_type = in.readInt();
        startTime = in.readLong();
        duration = in.readInt();
        distance = in.readInt();
        avgBpm = in.readInt();
        count = in.readInt();
        calories = in.readInt();
        speed = in.readInt();
        laps = in.readInt();
        validDuration = in.readInt();
        swolf = in.readInt();
        avgSpm = in.readInt();
    }

    public static final Creator<Mix_sport_Struct> CREATOR = new Creator<Mix_sport_Struct>() {
        @Override
        public Mix_sport_Struct createFromParcel(Parcel in) {
            return new Mix_sport_Struct(in);
        }

        @Override
        public Mix_sport_Struct[] newArray(int size) {
            return new Mix_sport_Struct[size];
        }
    };

    private static int toTimeS(int duration) {
        return duration / 100;
    }

    public Mix_sport_Struct() {
    }

    public Mix_sport_Struct(int sport_type, byte[] datas) {
        this.sport_type = sport_type;
    }

    public static Mix_sport_Struct parse(byte[] sport) throws Exception {
        ByteParser parser = new ByteParser(sport);
        Mix_sport_Struct struct = new Mix_sport_Struct();
        int sportType = parser.readInt(1);
        struct.setSport_type(sportType);
        struct.setStartTime(TimeUtil.s2CForDev(parser.readInt(4), true).getTimeInMillis());
        struct.setDuration(toTimeS(parser.readInt(4)));
        if (sportType == SPORT_TYPE_SWIMMING) {
            struct.setAvgBpm(parser.readInt(1));
            struct.setValidDuration(toTimeS(parser.readInt(4)));
            struct.setCount(parser.readInt(2));
            struct.setLaps(parser.readInt(2));
            struct.setSwolf(parser.readInt(2));
            struct.setDistance(parser.readInt(2));
            struct.setSpeed(parser.readInt(2));
            struct.setCalories(parser.readInt(2));
            struct.setAvgSpm(parser.readInt(2));
        } else if (sportType == SPORT_TYPE_RUN || sportType == SPORT_TYPE_HIKE) {
            struct.setSpeed(parser.readInt(2));
            struct.setDistance(parser.readInt(4));
            struct.setAvgBpm(parser.readInt(1));
            struct.setCalories(parser.readInt(2));
            struct.setCount(parser.readInt(4));
        } else if (sportType == SPORT_TYPE_WEIGHT || sportType == SPORT_TYPE_BENCH ||
                sportType == SPORT_TYPE_SIT_UP || sportType == SPORT_TYPE_SKIPPING ||
                sportType == SPORT_TYPE_CLIMB) {
            struct.setAvgBpm(parser.readInt(1));
            struct.setCount(parser.readInt(4));
            struct.setCalories(parser.readInt(2));
        } else {
            throw new Exception("未知类型:" + sportType);
        }
        return struct;
    }

    public int getSport_type() {
        return sport_type;
    }

    public void setSport_type(int sport_type) {
        this.sport_type = sport_type;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public int getAvgBpm() {
        return avgBpm;
    }

    public void setAvgBpm(int avgBpm) {
        this.avgBpm = avgBpm;
    }

    public int getCalories() {
        return calories;
    }

    public void setCalories(int calories) {
        this.calories = calories;
    }

    public int getValidDuration() {
        return validDuration;
    }

    public void setValidDuration(int validDuration) {
        this.validDuration = validDuration;
    }

    public int getLaps() {
        return laps;
    }

    public void setLaps(int laps) {
        this.laps = laps;
    }

    public int getSwolf() {
        return swolf;
    }

    public void setSwolf(int swolf) {
        this.swolf = swolf;
    }

    public int getAvgSpm() {
        return avgSpm;
    }

    public void setAvgSpm(int avgSpm) {
        this.avgSpm = avgSpm;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Mix_sport_Struct{" +
                "sport_type=" + sport_type +
                ", startTime=" + startTime +
                ", duration=" + duration +
                ", distance=" + distance +
                ", avgBpm=" + avgBpm +
                ", count=" + count +
                ", calories=" + calories +
                ", speed=" + speed +
                ", laps=" + laps +
                ", validDuration=" + validDuration +
                ", swolf=" + swolf +
                ", avgSpm=" + avgSpm +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(sport_type);
        dest.writeLong(startTime);
        dest.writeInt(duration);
        dest.writeInt(distance);
        dest.writeInt(avgBpm);
        dest.writeInt(count);
        dest.writeInt(calories);
        dest.writeInt(speed);
        dest.writeInt(laps);
        dest.writeInt(validDuration);
        dest.writeInt(swolf);
        dest.writeInt(avgSpm);
    }
}
